package com.ccs.testdatas;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class Billing {

	public void select_discharge(WebDriver driver, String option){
		
		if(option.equals("das")){
			driver.findElement(By.xpath("//input[@id='dischargeType2']")).click();
			driver.findElement(By.xpath("//input[@id='submitFormButton']")).click();
			driver.findElement(By.xpath("//div[@id='RoomBoardPostingPopup']/div[2]/input[1]")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			System.out.println("Patient DAS Discharged.");
		}else{
			driver.findElement(By.xpath("//input[@id='dischargeType1']")).click();
			driver.findElement(By.xpath("//input[@id='submitFormButton']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
		
	}
	
	public void goto_discharge_home(WebDriver driver, String pin){
		
		driver.manage().window().maximize();
		try{
			driver.findElement(By.xpath("//a[@href='/pba/pbaHome.html']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}catch(Exception e){}
		driver.findElement(By.xpath("//input[@id='criteria']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='search']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		Select action = new Select(driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[8]/select")));
		action.selectByVisibleText("Discharge Patient");
		driver.findElement(By.xpath("//table[@id='results']/tbody/tr/td[9]/input")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	
	public void regular_discharge(WebDriver driver, String option){
		
		//SKIP GUARANTOR
		driver.findElement(By.xpath("//input[@name='formAction']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP ROOM
		driver.findElement(By.xpath("//input[@name='skipButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP PHILHEALTH
		driver.findElement(By.xpath("//input[@id='btnSkip']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP DISCOUNT
		driver.findElement(By.xpath("//input[@id='skipBtn']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		//SKIP GENERATION OF UNOFFICIAL SOA
		driver.findElement(By.xpath("//input[@name='submitButton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		if(option.equals("cash")){
			
			driver.findElement(By.xpath("//input[@id='cashPaymentMode1']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			String payment = driver.findElement(By.xpath("//span[@id='totalAmountDue']")).getText();
			System.out.println("Amount Due: "+ payment);
			if(payment.contains(",")){
				payment = payment.replaceAll(",","");
			}
			driver.findElement(By.xpath("//input[@id='cashBillAmount']")).sendKeys(payment);
			driver.findElement(By.xpath("//input[@value='Proceed with Payment']")).click();
			
			driver.findElement(By.xpath("//input[@id='admissionDoctorsPayment0.pfPayment']")).clear();
			driver.findElement(By.xpath("//input[@id='admissionDoctorsPayment0.pfPayment']")).sendKeys("100");
			driver.findElement(By.xpath("//input[@id='cashPaymentMode1']")).click();
			driver.findElement(By.xpath("//input[@id='cashBillAmount']")).sendKeys("100");
			driver.findElement(By.xpath("//input[@value='Proceed with Payment']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			
		}else if (option.equals("hmo")){
			
			driver.findElement(By.xpath("//input[@value='Print SOA']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@value='Generate Official SOA']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			
			//GENERATE OFFICIAL SOA
			driver.findElement(By.xpath("//input[@id='showHeader']")).click();
			driver.findElement(By.xpath("//input[@value='Submit']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
			
		driver.findElement(By.xpath("//input[@value='Go To Landing Page']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		System.out.println("Patient Bill Discharged.");
	}
	
	public void update_guarantor(WebDriver driver, String pin){
		
		//NOTE
		//Make sure that the method "goto_discharge_home" is called first before calling this method
		
		driver.findElement(By.xpath("//table[@id='row']/tbody/tr/td[15]/input")).click();
		driver.findElement(By.xpath("//a[@id='updateLink']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		double num =  Math.floor((Math.random()*9999)+1);
		String code = String.valueOf(num);
		code = code.replaceAll("\\.","");
		
		//UPDATE GUARANTOR PAGE
		String hmoForm = driver.getWindowHandle();
		driver.switchTo().window(hmoForm);
		
		driver.findElement(By.xpath("//input[@id='includePfTag']")).click();
		driver.findElement(By.xpath("//input[@id='chkCovered0']")).click();
		driver.findElement(By.xpath("//input[@id='loa.loaNo']")).clear();
		driver.findElement(By.xpath("//input[@id='loa.loaNo']")).sendKeys("loa"+code);
		
		//driver.findElement(By.xpath("//input[@id='loa.percentageLimit']")).click();
		WebElement percent = driver.findElement(By.xpath("//input[@id='loa.percentageLimit']"));
		percent.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),"100");
		WebElement pfAmount = driver.findElement(By.xpath("//input[@id='loa.maximumPfAmount']"));
		pfAmount.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),"1000");
		
		//SUBMIT FORM
		driver.findElement(By.xpath("//div[@class='pbaFormFooter']/input[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		//GOBACK TO PATIENT AND BILLING PAGE
		driver.findElement(By.xpath("//a[contains(.,'Patient Billing and Accounting Home')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	
}
